<?php
/**
 * 前台栏目控制器
 */
class CategoryAction extends CommonAction {

	protected $currentModel=null;
	protected $cacheData=array();

	public function _initialize()
	{
		parent::_initialize();
		$this->currentModel = D(MODULE_NAME);
		$this->cacheData=get_cache(MODULE_NAME);
	}	
	
	public function index()
	{
		$id=$this->_get($this->currentModel->getPk());
		if(!isset($id)||!is_numeric($id)){$this->error('非法操作','/');}
		$categoryWhere['id']=$id;
		$categoryWhere['model']='Article';
		$data=$this->currentModel->where($categoryWhere)->find();
	  	if(empty($data))
	  	{
		  $this->error('数据不存在','/');
	  	}
    	$seo=seo($data);
    	$this->assign('pos',get_pos($data['id'],$this->cacheData));
    	$this->assign('seo',$seo);
    	$this->assign('data',$data);
    	$categoryChild=$this->getChildCategory($data['id']);
    	if(empty($categoryChild))
    	{
    		$this->lists($data);
    	}
	}

	private function getChildCategory($id)
	{
		$child=array();
		foreach($this->cacheData as $d)
		{
			if($d['parent_id']==$id)$child=$d;
		}
		return $child;
	}


	private function lists($categoryInfo)
	{
		import ('@.ORG.Page');
    	$map['category_id']=$categoryInfo['id'];
    	$map['stauts']=1;
    	$dataModel=D(ucfirst($categoryInfo['model']));
    	$count=$dataModel->where($map)->count();
    	$page = new Page($count,C ('PAGE_LIMIT'));
    	$datas=$dataModel->where($map)->relation('Comment')->limit($page->firstRow.','.$page->listRows)->order('id DESC')->select();
    	$this->assign('page',$page->show());
    	$this->assign('datas',$datas);    		
		$this->display(strtolower($categoryInfo['model']).'_lists');
	}   
}